package Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/cookieservlet2")
public class cookIndex2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.print("<h1>Hello cookie</h1>");
        Cookie[] cookies=req.getCookies();
        boolean flag=false;
        String cok1="";
        String cok2="";
        for (Cookie c:cookies) {
            if (c.getName().equals("name")) {
                cok1=c.getValue();
                flag=true;
            }
            if (c.getName().equals("pwd")) {
                cok2=c.getValue();
                flag=true;
            }
        }
        if (flag) {
            out.print("<h1>欢迎再次登录</h1>");
            System.out.println("name:"+cok1+"-----"+"pwd:"+cok1);
        }
        else {
            out.print("<h1>对不起，请您重新登录</h1>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
